/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.dao;

import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.delivery.dto.Reason;

public class ReasonDAOHibernate extends HibernateDaoSupport implements ReasonDAO {

	@Override
	public void saveReason(Reason reason) {
		getHibernateTemplate().saveOrUpdate(reason);
	}
	
	@SuppressWarnings("unchecked")
	public long getReasonCount(String orderId) {
		final String queryString = "select count(r.id) from Reason r where r.orderId=?";
		List result = getHibernateTemplate().find(queryString, orderId);
		long count = 0;
		if (result.size() > 0) {
			count = (Long)result.get(0);
		}
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public List<Reason> getReasonList(String orderId) {
		final String queryString = "from Reason r where r.orderId=?";
		List result = getHibernateTemplate().find(queryString, orderId);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Reason> getReasonList(Date beginDate, Date endDate) {
		final String queryString = "from Reason r " +
				"where r.createDate between ? and ? order by r.category, r.createDate";
			List result = getHibernateTemplate().find(queryString, new Object[]{
														beginDate, endDate});
			return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Reason> getReasonList(Date beginDate, Date endDate, int deptId) {
		final String queryString = "from Reason r " +
				"where r.createDate between ? and ? and r.category=? order by r.createDate";
		List result = getHibernateTemplate().find(queryString, new Object[]{
														beginDate, endDate, deptId});
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public long getReasonCount(Date beginDate, Date endDate, int deptId) {
		final String queryString = "select count(r.id) from Reason r " +
				"where r.createDate between ? and ? and r.category=?";
		List result = getHibernateTemplate().find(queryString, new Object[]{
				beginDate, endDate, deptId
		});
		long count = 0;
		if (result.size() > 0) {
			count = (Long)result.get(0);
		}
		return count;
	}

}
